home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7848 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: very simple newbie question
  5. Date: 28 Feb 1996 16:04:44 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb28090444@qcd.lanl.gov>
  8. References: <robertk-2802960012350001@robertk.accessone.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: robertk@accessone.com's message of 28 Feb 1996 07:13:20 GMT
  13.  
  14. In article <robertk-2802960012350001@robertk.accessone.com>
  15. robertk@accessone.com (robertk) writes:
  16. <snip>
  17. r:              #include <stdio.h>
  18. r:              main()
  19. r:              {
  20. r:               char s[10];
  21. r:               scanf("%s/n",s);
  22. r:               printf("%s",s);
  23. r:              }
  24. r: 
  25. r:             when i run this and input:
  26. r: 
  27. r:                   robert <enter> --->when i hit the enter key the cursor 
  28. r:                                      drops down a line and sits
  29. ther flashing 
  30. r:                                      waiting for me to enter
  31. another character 
  32. r:                                      like x before it will throw me
  33. back to the 
  34. r:                                      ide. upon returning to the
  35. screen i see 
  36. r:                   robert
  37. r:                   x
  38. r:                   robert
  39. r: 
  40. r:                i thought scanf() ignored whitespace?
  41.  
  42.  
  43. First, /n is not white space, it is the character '/' followed by the
  44. character 'n'. When /n appears as a part of the format string for
  45. scanf, it tells scanf to read and discard the characters / and n if
  46. they follow. As the end of line is not a /, the behaviour is not
  47. consistent with your code. Check your code before wasting people's
  48. time with it.
  49.  
  50. Next, whitespace is not ignored in scanf. It means read and discard
  51. all following whitespace. So, scanf has to continue reading till it
  52. finds a non-whitespace character, which it leaves unread. This
  53. explains the behaviour that you see.
  54.  
  55. Cheers
  56. Tanmoy
  57. --
  58. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  59. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  60. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  61. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  62. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  63. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  64.